<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";

const animationDuration = 6000;

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "400px"
    },
    totalSales: {
      type: String,
      default: "0"
    },
    dataList: {
      type: [Object, Array],
      default: () => {}
    },
    dataValue: {
      type: [Object, Array],
      default: () => {}
    },
    dataX: {
      type: [Object, Array],
      default: () => {}
    },
    xValue:{
      type: String,
      default: ""
    }
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
      console.log(this.dataX)
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.chart.setOption({
        tooltip: {
          trigger: "axis"
        },
        legend: {
          data: this.dataList
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true
        },
        toolbox: {
          feature: {
            saveAsImage: {}
          }
        },
        xAxis: {
          name: this.xValue,
          type: "category",
          boundaryGap: false,
          data: this.dataX
        },
        yAxis: [
          {
            axisLabel: {
              formatter: function() {
                return "";
              }
            }
          }
        ],
        series: this.dataValue
      });
    }
  }
};
</script>
